import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {
    public static int findSubstringInWraproundString(String s) {
        int len = s.length();

        //以i为结尾的子字符串在s中的个数
        int[] dp = new int[len];

        //初始化
        for (int i = 0; i < len; i++) {
            dp[i] = 1;
        }

        for (int i = 1; i < len; i++) {
            if (s.charAt(i - 1) + 1 == s.charAt(i) || (s.charAt(i - 1) == 'z' && s.charAt(i) == 'a')) {
                dp[i] += dp[i - 1];
            }
        }

        int sum = 0;

        HashMap<Character, Integer> map = new HashMap<>();

        //去重，遇到相同结尾的字串，选择dp值较大的
        for (int i = 0; i < len; i++) {
            int temp = map.getOrDefault(s.charAt(i), 0);
            map.put(s.charAt(i), Math.max(temp, dp[i]));
        }

        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            sum += entry.getValue();
        }

        return sum;

    }

    public static void main(String[] args) {
        System.out.println(findSubstringInWraproundString("zab"));
    }
}
